package com.example.yrdev.ordering_system.Fragment;

import android.app.Fragment;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.HorizontalScrollView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.yrdev.ordering_system.Database.DingHanDB;
import com.example.yrdev.ordering_system.Model.CustomViewHolder;
import com.example.yrdev.ordering_system.Model.PurchaseReport;
import com.example.yrdev.ordering_system.R;
import com.example.yrdev.ordering_system.Utils.GlobalFunction;

import java.util.ArrayList;

/**
 * Created by luke on 12/12/2016.
 */


    public class PurchaseDetailFragment extends Fragment{//消费明细
        Context context;
        ArrayList<PurchaseReport> arrayList;
        int payModeType=0;

     public    interface  Callback{
            void selectDate();

        }
    private  Callback callback;

    public void setCallback(Callback callback) {
        this.callback = callback;
    }

    public void setContext(Context context) {
            this.context = context;
        }

        public void setArrayList(ArrayList<PurchaseReport> arrayList) {

            this.arrayList = arrayList;



        }

    public void setPayModeType(int payModeType) {

           this.payModeType = payModeType;
            checkDdateInterval(currentStartDate,currentEndDate);

    }

    public  ArrayList<PurchaseReport> getCurrentReport(){

        return  correspondList;


    }

    private ArrayList<PurchaseReport>correspondList;

    public void checkDdateInterval(String startDate,String returnDate){

        currentStartDate=startDate;
         currentEndDate=returnDate;


        try {
            intervalDate.setText(startDate+"-"+returnDate);
            ArrayList<PurchaseReport >filteredReport=  PurchaseReport.getPurchaseReportOnTypeAndInterval(getActivity(),payModeType,startDate,returnDate);
        correspondList=filteredReport;


            if (filteredReport.size()>0){

                showContent(true);
                adapter=new DetailedAdapter(getActivity(),filteredReport);
                listView.setAdapter(adapter);
                adapter.notifyDataSetChanged();


            }else {

                showContent(false);
            }





        }catch (Exception e){

//            GlobalFunction.printTextByTag(e.getMessage());
        }
    }

    private  void  showContent(boolean stat){
        if (stat){
           content.setVisibility(View.VISIBLE);
            noData.setVisibility(View.GONE);
        }else {
            content.setVisibility(View.GONE);
            noData.setVisibility(View.VISIBLE);
        }
    }




       ListView listView;
     TextView intervalDate;
     DetailedAdapter adapter;
     String currentStartDate;
     String  currentEndDate;

    HorizontalScrollView content;
    LinearLayout noData;

        @Nullable
        @Override
        public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

            View view=inflater.inflate(R.layout.purchase_commodity_container_2,null);

            //初始化
            listView=(ListView)view.findViewById(R.id.report_list);
            intervalDate=(TextView)view.findViewById(R.id.intervalDate);

             currentStartDate=GlobalFunction.getCurrentDate();
             currentEndDate=GlobalFunction.getCurrentDate();

            content=(HorizontalScrollView)view.findViewById(R.id.report_container);
            noData=(LinearLayout)view.findViewById(R.id.noData);


            RelativeLayout selectDate=(RelativeLayout) view.findViewById(R.id.selectDate);
            selectDate.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    if (callback!=null)
                        callback.selectDate();
                }
            });
            return view;

        }
        public  static  class DetailedAdapter extends BaseAdapter {
            Context context;
            ArrayList<PurchaseReport>arrayList;

            public DetailedAdapter(Context context, ArrayList<PurchaseReport>arrayList){

                this.context=context;
                this.arrayList=arrayList;

            }

            public void setArrayList(ArrayList<PurchaseReport> arrayList) {
                this.arrayList = arrayList;
            }

            public ArrayList<PurchaseReport> getArrayList() {
                return arrayList;
            }

            public void reset(ArrayList<PurchaseReport> arrayList){

                setArrayList(arrayList);
                notifyDataSetChanged();

            }
            @Override
            public Object getItem(int position) {
                return arrayList.get(position);

            }

            @Override
            public long getItemId(int position) {
                return 0;
            }

            @Override
            public int getCount() {
                return arrayList.size();
            }

            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                LayoutInflater layoutInflater=LayoutInflater.from(context);

                PurchaseReport goods=arrayList.get(position);

                View view=convertView;
                CustomViewHolder viewHolder;

                if (convertView==null){

                    view=layoutInflater.inflate(R.layout.return_good_table_row,null);

                    viewHolder=new CustomViewHolder();
                    viewHolder.setTick((ImageView)view.findViewById(R.id.tick));
                    viewHolder.setIndex((TextView)view.findViewById(R.id.index));
                    viewHolder.setGoods_ticketID((TextView)view.findViewById(R.id.goods_ticketID));
                    viewHolder.setGoods_code((TextView)view.findViewById(R.id.goods_code));
                    viewHolder.setGoods_name((TextView)view.findViewById(R.id.goods_name));
                    viewHolder.setGoods_purchDate((TextView)view.findViewById(R.id.goods_PurchaseDate));
                    viewHolder.setGoods_amount((TextView)view.findViewById(R.id.goods_amount));
                    viewHolder.setGood_RetailPrice((TextView)view.findViewById(R.id.goods_retail_price));
                    viewHolder.setGood_totalPrice((TextView)view.findViewById(R.id.goods_totalPrice));

                    //    viewHolder.setGood_PaymentType((TextView)view.findViewById(R.id.goods_paymentType));
                    view.setTag(viewHolder);

                    //    GlobalFunction.printTextByTag("11"+goods.getItem_ID()+" "+goods.getItem_name());
                }


            viewHolder=(CustomViewHolder)view.getTag();

                if (goods.getUploadMark()==0){
                    viewHolder.getTick().setVisibility(View.GONE);
                }else {
                    viewHolder.getTick().setVisibility(View.VISIBLE);
                }

                viewHolder.getIndex().setText(position+1+"");
                viewHolder.getGoods_ticketID().setText(goods.getTicketID());
                viewHolder.getGoods_code().setText(goods.getCommodityID());
                viewHolder.getGoods_name().setText(goods.getCommodityName());
                viewHolder.getGoods_purchDate().setText(goods.getDate());
                viewHolder.getGoods_amount().setText(String.valueOf(goods.getVolume()));
                viewHolder.getGood_RetailPrice().setText(String.valueOf(goods.getSaleCost())+"元");
                viewHolder.getGood_totalPrice().setText(String.valueOf(goods.getCost())+"元");


                // viewHolder.getGood_PaymentType().setText(goods.getPayModeText());


                return view;

            }
        }



    }


